package com.maigao.util;

import com.apistd.uni.Uni;
import com.apistd.uni.UniException;
import com.apistd.uni.UniResponse;
import com.apistd.uni.sms.UniMessage;
import com.apistd.uni.sms.UniSMS;
import com.maigao.entity.enums.SmsTemplate;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: maigao
 * @description:
 * @author: yongza
 * @create: 2021-10-09 16:26
 **/
public class SmsUtil {
    public static final Logger log = Logger.getLogger(SmsUtil.class);
    private static String ACCESS_KEY_ID = "RqFBPUgUMkPWPwbFbREyrSRsF1NA7YcP2ViEaSvVK6UcQC6G5";
    private static final String SIGNATURE = "麦子网络";
    private static final Integer TTL_MINUTES = 10;

    public static boolean sendMsg(String mobile, Map<String, String> templateData, SmsTemplate template) {
        Uni.init(ACCESS_KEY_ID);
        UniMessage message = UniSMS.buildMessage()
                .setTo(mobile)
                .setSignature(SIGNATURE)
                .setTemplateId(template.getValue())
                .setTemplateData(templateData);
        try {
            UniResponse response = message.send();
            if (response.code.equals("0")) {
                return true;
            }
        } catch (UniException e) {
            log.error(e);
        }
        return false;
    }

    public static void sendLoginMsg(String mobile, HttpSession session) {
        Map<String, String> templateData = new HashMap<>(2);
        templateData.put("code", randomGetCode(mobile, session));
        templateData.put("ttl", String.valueOf(TTL_MINUTES));
        sendMsg(mobile, templateData, SmsTemplate.LOGIN);
    }
    public static void sendLoginMsg(String mobile,String code) {
        Map<String, String> templateData = new HashMap<>(2);
        templateData.put("code", code);
        templateData.put("ttl", String.valueOf(TTL_MINUTES));
        sendMsg(mobile, templateData, SmsTemplate.LOGIN);
    }

    public static void sendRegisterMsg(String mobile,String code) {
        Map<String, String> templateData = new HashMap<>(2);
        templateData.put("code",code);
        templateData.put("ttl", String.valueOf(TTL_MINUTES));
        sendMsg(mobile, templateData, SmsTemplate.REGISTER);
    }
    public static void sendRegisterMsg(String mobile, HttpSession session) {
        Map<String, String> templateData = new HashMap<>(2);
        templateData.put("code", randomGetCode(mobile, session));
        templateData.put("ttl", String.valueOf(TTL_MINUTES));
        sendMsg(mobile, templateData, SmsTemplate.REGISTER);
    }

    private static String randomGetCode(String mobile, HttpSession session) {
        String code = RandomStringUtils.randomNumeric(6);
        if (null != session) {
            session.setAttribute(mobile, code);
            session.setMaxInactiveInterval(TTL_MINUTES * 60);
        }
        return code;
    }

//    public static void main(String[] args) {
//        sendRegisterMsg("18221686090", );
//    }
}
